class Plan {
  int? id;
  String title;
  String description;
  String picture;
  DateTime startTime;
  DateTime endTime;
  String state; // 'completed', 'not_started', 'in_progress'
  int userId;

  Plan({
    this.id,
    required this.title,
    required this.description,
    required this.picture,
    required this.startTime,
    required this.endTime,
    required this.state,
    required this.userId,
  });

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'picture': picture,
      'startTime': startTime.millisecondsSinceEpoch,
      'endTime': endTime.millisecondsSinceEpoch,
      'state': state,
      'userId': userId,
    };
  }

  factory Plan.fromMap(Map<String, dynamic> map) {
    return Plan(
      id: map['id'],
      title: map['title'],
      description: map['description'] ?? '',
      picture: map['picture'],
      startTime: DateTime.fromMillisecondsSinceEpoch(map['startTime']),
      endTime: DateTime.fromMillisecondsSinceEpoch(map['endTime']),
      state: map['state'],
      userId: map['userId'],
    );
  }

  Plan copyWith({
    int? id,
    String? title,
    String? description,
    String? picture,
    DateTime? startTime,
    DateTime? endTime,
    String? state,
    int? userId,
  }) {
    return Plan(
      id: id ?? this.id,
      title: title ?? this.title,
      description: description ?? this.description,
      picture: picture ?? this.picture,
      startTime: startTime ?? this.startTime,
      endTime: endTime ?? this.endTime,
      state: state ?? this.state,
      userId: userId ?? this.userId,
    );
  }

  // 检查计划是否已完成
  bool get isCompleted => state == 'completed';

  // 检查计划是否未开始
  bool get isNotStarted => state == 'not_started';

  // 检查计划是否进行中
  bool get isInProgress => state == 'in_progress';

  // 检查计划是否是今天的
  bool get isToday {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    final planStart = DateTime(startTime.year, startTime.month, startTime.day);
    final planEnd = DateTime(endTime.year, endTime.month, endTime.day);
    
    return (today.isAtSameMomentAs(planStart) || 
            today.isAtSameMomentAs(planEnd) ||
            (today.isAfter(planStart) && today.isBefore(planEnd)));
  }

  // 检查计划是否是本周的
  bool get isThisWeek {
    final now = DateTime.now();
    final startOfWeek = now.subtract(Duration(days: now.weekday - 1));
    final endOfWeek = startOfWeek.add(Duration(days: 6));
    
    final weekStart = DateTime(startOfWeek.year, startOfWeek.month, startOfWeek.day);
    final weekEnd = DateTime(endOfWeek.year, endOfWeek.month, endOfWeek.day, 23, 59, 59);
    
    return (startTime.isBefore(weekEnd) && endTime.isAfter(weekStart));
  }

  // 获取计划对应的星期几
  String get weekdayName {
    final weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    return weekdays[startTime.weekday - 1];
  }

  // 检查计划是否在过去30天内
  bool get isInLast30Days {
    final now = DateTime.now();
    final thirtyDaysAgo = now.subtract(Duration(days: 30));
    return endTime.isAfter(thirtyDaysAgo) && endTime.isBefore(now);
  }

  // 获取倒计时剩余时间
  Duration get remainingTime {
    final now = DateTime.now();
    if (now.isAfter(endTime)) {
      return Duration.zero;
    }
    return endTime.difference(now);
  }

  // 格式化倒计时显示
  String get formattedRemainingTime {
    final remaining = remainingTime;
    if (remaining == Duration.zero) {
      return 'Expired';
    }
    
    final days = remaining.inDays;
    final hours = remaining.inHours % 24;
    final minutes = remaining.inMinutes % 60;
    
    if (days > 0) {
      return '${days}d ${hours}h ${minutes}m';
    } else if (hours > 0) {
      return '${hours}h ${minutes}m';
    } else {
      return '${minutes}m';
    }
  }
}
